// Copyright 2007 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lichen.pages.topic;

import java.util.List;

import lichen.entities.forum.Forum;
import lichen.entities.post.AbstractPost;
import lichen.entities.post.Post;
import lichen.entities.post.Topic;
import lichen.pages.base.BasePage;
import lichen.pages.base.LogInfoPage;
import lichen.pages.base.MenuPage;
import lichen.services.menu.Menu;
import lichen.services.menu.impl.MenuImpl;
import lichen.services.post.PostService;

import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.annotations.Inject;
import org.apache.tapestry.corelib.components.Loop;

/**
 * 浏览一个贴子
 * 
 * @author <a href="mailto:jun.tsai@gmail.com">Jun Tsai</a>
 * @version $Revision: 28 $
 * @since 0.0.1
 */
public class ViewTopic extends BasePage  implements LogInfoPage,MenuPage{

	/** 默认浏览模式，平板模式**/
	public static final int FLAT_VIEW = 1;
	/** 树形结构展示 **/
	public static final int TREE_VIEW = 2;
	/** 查询某一个主题的贴子 **/
	private static final CharSequence QUERY_POSTS_HQL = "from "
		+ AbstractPost.class.getName()
		+ " p where p."+AbstractPost.PARENT_TOPIC_PRO_NAME+"=?  ";
	
	/** 当前浏览的主题 **/
	private Topic topic;
	/** 当前主题所在的论坛 **/
	private Forum forum;

	/** 循环使用到的贴子 **/
	private Post post;
	
	/** Post Service **/
	@Inject
	private PostService postService;

	/** 浏览的方式 **/
	private int viewMethod;
	
	@SuppressWarnings("unused")
	@Component(parameters={"source=posts","value=post"})
	private Loop postsLoop;

	private int lastLevel=-1;
	
	private int currentLevel;
	private String suffixIndented;

	
	/**
	 * 得到当前的贴子
	 * @return Returns the post.
	 */
	public Post getPost() {
		return post;
	}

	/**
	 * 设定贴子
	 * @param post
	 *            The post to set.
	 */
	public void setPost(Post post) {
		
		this.post = post;
		this.post.setParentTopic(this.topic);
		if(!this.isTreeView()){
			return;
		}
		this.suffixIndented ="";
		this.currentLevel = this.post.getThread().split("\\.").length;
		if(lastLevel<0||lastLevel ==1){ // 第一次 或者是上次为顶级的节点
			//do nothing
		}else if (lastLevel >= currentLevel){
			StringBuffer sb=new StringBuffer();
			while(lastLevel>=currentLevel){
				sb.append("</div>");
				lastLevel--;
			}
			this.suffixIndented=sb.toString();
		}
		lastLevel=currentLevel;
		
		
	}
	public String getSuffixIndented(){
		return suffixIndented;
	}
	public String getLastIndented(){
		if(!this.isTreeView()){
			return "";
		}
		StringBuffer sb=new StringBuffer();
		while(lastLevel>1){
			sb.append("</div>");
			lastLevel--;
		}
		return sb.toString();
	}
	public String getPrefixIndented(){
		return this.currentLevel>1?"<div class=\"indented\">":"";
	}

	
	void onActivate(Topic topic,int viewMethod) {
		this.topic = topic;
		this.viewMethod = viewMethod;
		
		postService.increamViewNum(topic);
		this.forum = this.topic.getForum();
	}

	Object[] onPassivate() {
		return new Object[]{this.topic,this.viewMethod};
	}



	/**
	 * @return Returns the topic.
	 */
	public Topic getTopic() {
		return topic;
	}

	/**
	 * @param topic The topic to set.
	 */
	public void setTopic(Topic topic) {
		this.topic = topic;
	}

	public Forum getForum() {
		return this.forum;
	}

	
	
	public List getPosts() {
		StringBuffer sb=new StringBuffer(QUERY_POSTS_HQL);
		if(this.viewMethod == TREE_VIEW){
			sb.append(" order by p."+AbstractPost.THREAD_PRO_NAME+" desc");
		}else{
			sb.append(" order by p.");
			sb.append(AbstractPost.CREATE_DATE_PRO_NAME);
		}
		return this.getHibernateTemplate().find(sb.toString(), this.topic);
	}

	public Object[] getCommentParameters() {
		return new Object[] { this.post};
	}
	void onActionFromFlatView(){
		this.viewMethod=FLAT_VIEW;
	}
	void onActionFromTreeView(){
		this.viewMethod=TREE_VIEW;
	}
	public boolean isTreeView(){
		return this.viewMethod == TREE_VIEW;
	}

	/**
	 * 
	 * @see lichen.pages.base.LogInfoPage#getLogInfo()
	 */
	public String getLogInfo() {
		return getMessages().format("topic.view",topic.getTitle());
	}

	public List<Menu> contributeMenus() {
		List<Menu> menus = this.getMenuService().generateForumMenu(forum,null);
		StringBuffer action=new StringBuffer();
		action.append(getMessages().format("topic.view",topic.getTitle()));
		menus.add(MenuImpl.newInstance(action.toString()));
		return menus;
	}
}
